﻿import mx.data.encoders.Bool;
import mx.data.types.Str;
dynamic class MyCheckBox extends MovieClip {
	private static var checkBoxGroup:Object = new Object();
	
	public static function clearGroup(groupid:String) {
		checkBoxGroup[groupid].cb = new Array();
	}
	public static function getGroupSelecteItem(groupid:String):MyCheckBox {
		return checkBoxGroup[groupid].selectItem;
	}
	public static function GetGroup(groupid:String):Array {
		return checkBoxGroup[groupid].cbs;
	}
	
	private var CheckBoxGroupID:String = "";
	public function get CheckGroup():String {
		return this.CheckBoxGroupID;
	}
	public function set CheckGroup(value:String) {
		if (value == undefined || value == null ) return;
		if (CheckBoxGroupID != "") {
			var cbs:Array = checkBoxGroup[CheckBoxGroupID].cbs;
			var index = -1;
			for (var i = 0; i < cbs.length; i++) {
				if (cbs[i] == this) {
					index = i;
				}
			}
			if (index >= 0) {
				cbs = cbs.slice(0, index).concat(cbs.slice(index + 1, cbs.length));
			}
			this.Checked = false;
		}
		this.CheckBoxGroupID = value;
		if (checkBoxGroup[value] != undefined) {
			var cbs:Array = checkBoxGroup[value].cbs;
			for (var i = 0; i < cbs.length; i++) {
				if (cbs[i] == this) {
					return;
				}
			}
			cbs.push(this);
		}else {
			checkBoxGroup[value] = new Object();
			checkBoxGroup[value].cbs = new Array();
			checkBoxGroup[value].cbs.push(this);
			checkBoxGroup[value].selectItem = null;
		}
	}
	private var _checked:Boolean;
	public function get Checked():Boolean {
		return _checked;
	}
	/**
	 * if item is checked
	 */
	public function set Checked(value:Boolean) {
		if(MyCheckBox(checkBoxGroup[this.CheckBoxGroupID].selectItem)!=this){
			MyCheckBox(checkBoxGroup[this.CheckBoxGroupID].selectItem).Checked = false;
		}
		if (MyCheckBox(checkBoxGroup[this.CheckBoxGroupID].selectItem) == this && !value) {
			checkBoxGroup[this.CheckBoxGroupID].selectItem = null;
		}
		if(value){
			checkBoxGroup[this.CheckBoxGroupID].selectItem = this;
		}
		this._checked = (value == true);
		setChecked(value);
	}
	/**
	 * the checked frame label name
	 */
	public var CheckFrameLabel:String = "checked";
	/**
	 * the unchecked frame label name
	 */
	public var UnCheckFrameLabel:String = "unchecked";
	
	public function get Group():Array {
		return checkBoxGroup[this.CheckBoxGroupID].cbs;
	}
	public function get groupSelectedItem():MyCheckBox {
		return checkBoxGroup[this.CheckBoxGroupID].selectItem;
	}
	/**
	 * jump to right frame
	 * @param	selected if selected
	 */
	private function setChecked(selected:Boolean) {
		if (selected) {
			this.gotoAndPlay("checked");
		}else {
			this.gotoAndPlay("unchecked")
		}
	}
	
	
	private function sayf(o:Object) {
		trace("checkbox:" + o);
	}
}